<?php

/**
 * View helper to generate links to module resources.
 *
 * @author     Mon Zafra <monzee at gmail>
 * @copyright  (c)2009 Mon Zafra
 * @category   Mz
 * @package    Mz_Module
 * @subpackage Helper
 * @license    http://mz-project.googlecode.com/svn/trunk/LICENSE    MIT License
 * @version    SVN: $Id: Resource.php 6 2009-02-20 11:44:33Z monzee $
 */
class Mz_Module_Helper_Resource
{
    /**
     * Generates a link.
     *
     * @param string $resource Resource name
     * @param string $module   Needed if you render a partial outside the
     *                         current module
     * @return string
     */
    public function resource($resource, $module = null)
    {
        $resource = ltrim($resource, '/');

        $front = Zend_Controller_Front::getInstance();
        $request = $front->getRequest();
        $baseUrl = $request->getBaseUrl();

        if (null === $module) {
            $module = $request->getModuleName();
        }

        if ($front->hasPlugin('Mz_Module_Initializer')) {
            $plugin = $front->getPlugin('Mz_Module_Initializer');
            $module = $plugin->getModule($module);

            return $module->urlToResource($resource, $baseUrl);
        }

        return $baseUrl . '/' . $resource;
    }

}
